<!DOCTYPE html>
<html lang="zh_CN"
      xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
    <!--公用头部-->
    <th:block th:replace="common/head::commonHead(文件管理-小邑)"/>
    <style>
        .layui-table-click .project-list-item {
            /*background-color: #FAFAFA !important;*/
            background-color: #fff3e0 !important;
        }

        .layui-laypage .layui-laypage-curr .layui-laypage-em {
            border-radius: 0px !important;
        }
    </style>
</head>
<!--公用js-->
<th:block th:replace="common/head::commonJs"/>
<!--pdf在线-->
<script th:src="@{/static/admin/js/pdfjs/pdfobject.min.js}" charset="utf-8"></script>
<body marginwidth="0" marginheight="0">
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-header">当前位置：<span id="tv-current-position">/</span></div>
        <div class="layui-card-body">
            <form class="layui-form" id="fileForm">
                <div class="layui-form">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <button type="button" data-type="back" class="xy-btn xy-btn-primary icon-btn xy-btn-sm">
                                <i class="layui-icon layui-font-12">&#xe65c;</i> 返回上级
                            </button>
                            <button type="button" data-type="refresh" class="xy-btn xy-btn-primary icon-btn xy-btn-sm">
                                <i class="layui-icon layui-font-12">&#xe68e;</i> 主页
                            </button>
                            <button  sec:authorize="hasPermission('/file/addFolder','file:addFolder')" type="button" data-type="addFolder"
                                    class="xy-btn xy-btn-primary icon-btn xy-btn-sm">
                                <i class="layui-icon layui-font-12">&#xe654;</i> 新建文件夹
                            </button>
                            <button sec:authorize="hasPermission('/file/addFile','file:addFile')" type="button" data-type="upload" class="xy-btn xy-btn-primary icon-btn xy-btn-sm">
                                <i class="layui-icon layui-font-12">&#xe681;</i> 上传文件
                            </button>
                            <button sec:authorize="hasPermission('/file/getFileTree','file:getFileTree')" type="button" data-type="config" class="xy-btn xy-btn-primary icon-btn xy-btn-sm">
                                <i class="layui-icon layui-font-12">&#xe653;</i> 目录管理
                            </button>
                            <button type="button" class="xy-btn xy-btn-primary icon-btn xy-btn-sm" id="order">
                                排序 <i class="layui-icon layui-icon-down layui-font-12"></i>
                            </button>
                        </div>
                    </div>
                </div>
            </form>
            <div id="currentTableId"></div>
        </div>
    </div>
</div>

<script th:inline="javascript">
    var rootPath = [[${T(com.yy.permissions_general.common.constant.SystemConstant).FILE_MODULE_FILE}]];
    var filePath = [[${T(com.yy.permissions_general.common.constant.SystemConstant).FILE_MODULE_FILE}]];
    var checkObj = "";
    var parentId = 0;
    var orderWay = "";
    var orderName = "";
    layui.use(['table', 'layer', 'form', 'jquery', 'fileTable', 'dropdown', 'contextMenu'], function () {
        let table = layui.table;
        let form = layui.form;
        let upload = layui.upload;
        let $ = layui.jquery;
        let layer = layui.layer;
        let dropdown = layui.dropdown;
        let contextMenu = layui.contextMenu;
        let fileTable = layui.fileTable;

        //自定义事件 - mousedown
        dropdown.render({
            elem: '#order'
            , trigger: 'mousedown'
            , data: [{
                title: '按文件名称排序'
                , id: 100
                , type: 'group'
                , isSpreadItem: false
                , child: [
                    {
                        title: '升序'
                        , id: "file_name-ASC"
                    }, {
                        title: '降序'
                        , id: "file_name-DESC"
                    }
                ]
            }, {
                title: '按文件修改时间排序'
                , id: 200
                , type: 'group'
                , isSpreadItem: false
                , child: [
                    {
                        title: '升序'
                        , id: "update_time-ASC"
                    }, {
                        title: '降序'
                        , id: "update_time-DESC"
                    }
                ]
            }, {
                title: '按文件类型排序'
                , id: 300
                , type: 'group'
                , isSpreadItem: false
                , child: [
                    {
                        title: '升序'
                        , id: "file_suffix-ASC"
                    }, {
                        title: '降序'
                        , id: "file_suffix-DESC"
                    }
                ]
            }, {
                title: '按文件大小排序'
                , id: 400
                , type: 'group'
                , isSpreadItem: false
                , child: [
                    {
                        title: '升序'
                        , id: "file_size-ASC"
                    }, {
                        title: '降序'
                        , id: "file_size-DESC"
                    }
                ]
            }]
            , click: function (data, othis) {
                //根据 id 做出不同操作
                var splitId = data.id.split("-");
                orderName = splitId[0];
                orderWay = splitId[1];
                searchTable(parentId);
            }
        });

        // var data = cardTable.getChecked("currentTableId"); 获取选中的数据
        // var data = cardTable.getAllData("currentTableId"); 获取所有数据
        function searchTable(initParentId) {
            fileTable.render({
                elem: '#currentTableId',
                url: basePath + "/file/list", // 接口数据
                where: {
                    parentId: initParentId,
                    orderName: orderName,
                    orderWay: orderWay,
                },
                data: [], // 静态数据
                page: true, // 是否分页
                limit: 36, // 每页数量
                linenum: 12, // 每行数量
                clickItem: function (data, obj) { // 单击事件
                    var selectIsDir = data.isDir;
                    var selectId = data.id;
                    var selectFileSuffix = data.fileSuffix;
                    var selectFilePath = data.filePath;
                    var selectFileName = data.fileName;
                    if (selectIsDir == 1) {
                        checkObj = data;
                    }
                    var x = $(obj).offset().left + 12.5;
                    var y = $(obj).offset().top + $(obj).outerHeight() - $('html,body').scrollTop() - 14;
                    /*鼠标右键*/
                    var items = [
                        {
                            icon: 'layui-icon layui-icon-search',
                            name: '查看',
                            click: function () {
                                if (selectIsDir == 1) {
                                    // 文件夹
                                    parentId = selectId;
                                    filePath = selectFilePath;
                                    var dir = "";
                                    if (parentId != 0) {
                                        dir = selectFilePath.substring(rootPath.length + 1);
                                    }
                                    $('#tv-current-position').text("/" + dir);
                                    searchTable(parentId);
                                } else {
                                    //文件
                                    downloadAttachCommon(selectFileName, selectFilePath, selectFileSuffix);
                                }
                            }
                        },
                        {
                            icon: 'layui-icon layui-icon-edit',
                            name: '重命名',
                            hidden: ([[${#authorization.expression('hasPermission("/file/editFolder","file:editFolder")')}]] && [[${#authorization.expression('hasPermission("/file/editFile","file:editFile")')}]])==true?false:true,
                            click: function () {
                                if (selectIsDir == 1) {
                                    // 文件夹
                                    layer.open({
                                        type: 2,
                                        id: "addFolder",
                                        skin: 'layer-ext-myskin',
                                        title: "文件夹重命名",
                                        shadeClose: false,
                                        shade: 0.3,
                                        area: ["460px", "245px"],
                                        content: basePath + "/view/system/file/file_add_update_folder?type=2&parentId=" + parentId + "&id=" + selectId,
                                        end: function () {
                                            searchTable(parentId);
                                        }
                                    });
                                } else {
                                    //文件
                                    layer.open({
                                        type: 2,
                                        id: "updateFile",
                                        skin: 'layer-ext-myskin',
                                        title: "文件重命名",
                                        shadeClose: false,
                                        shade: 0.3,
                                        area: ["460px", "245px"],
                                        content: basePath + "/view/system/file/file_add_update?parentId=" + parentId + "&id=" + selectId,
                                        end: function () {
                                            searchTable(parentId);
                                        }
                                    });
                                }
                            }
                        },
                        {
                            icon: 'layui-icon layui-icon-delete',
                            name: '删除',
                            hidden: [[${#authorization.expression('hasPermission("/file/delete","file:delete")')}]] == true?false:true,
                            click: function () {
                                layer.confirm('确定要删除吗？', {
                                    icon: 5,
                                    title: '提示',
                                    skin: 'layer-ext-myskin'
                                }, function (index) {
                                    layer.close(index);
                                    // 异步请求
                                    sendAjaxPostToError(
                                        basePath + '/file/delete',
                                        {
                                            id: selectId
                                        },
                                        function (result) {
                                            if (result.success) {
                                                top.layui.notice.msg(result.msg ,{icon: 1,position:"topCenter"});
                                                searchTable(parentId);
                                            } else {
                                                top.layui.notice.msg(result.msg ,{icon: 2,position:"topCenter"});
                                            }
                                        },
                                        function (data) {
                                            if (!utils.string.isEmpty(data) && !utils.string.isEmpty(data.msg)) {
                                                top.layui.notice.msg(data.msg ,{icon: 2,position:"topCenter"});
                                            } else {
                                                top.layui.notice.msg("服务器内部错误，请联系管理员~", {icon: 2,position:"topCenter"});
                                            }
                                        }
                                    )
                                });
                            }
                        },
                        {
                            icon: 'layui-icon layui-icon-download-circle',
                            name: '下载',
                            hidden: selectIsDir == 1 ? true : false,
                            click: function () {
                                window.location.href = basePath + '/downloadFile?filePath=' + selectFilePath + '&fileName=' + selectFileName;
                            }
                        },
                    ];
                    contextMenu.show(items, x, y);
                    event.stopPropagation();
                },
                done: function () {
                }
            })
        }

        searchTable(parentId);

        //通过class 筛选
        $('.xy-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
        //点击事件
        var active = {
            //返回上级
            back: function () {
                var cDir = $('#tv-current-position').text();
                if (cDir != '/') {
                    cDir = cDir.substring(0, cDir.lastIndexOf('/'));
                    cDir || (cDir = '/');
                    $('#tv-current-position').text(cDir);
                    if (checkObj.parentId == 0) {
                        parentId = 0;
                        filePath = rootPath;
                        searchTable(parentId);
                        return;
                    }
                    sendAjaxPostToError(
                        basePath + '/file/detailFile',
                        {id: checkObj.parentId},
                        function (result) {
                            if (result.success == true) {
                                parentId = result.data.id;
                                filePath = result.data.filePath;
                                checkObj = result.data;
                                searchTable(parentId);
                            } else {
                                top.layui.notice.msg(result.msg ,{icon: 2,position:"topCenter"});
                            }
                        },
                        function (data) {
                            if (!utils.string.isEmpty(data) && !utils.string.isEmpty(data.msg)) {
                                top.layui.notice.msg(data.msg ,{icon: 2,position:"topCenter"});
                            } else {
                                top.layui.notice.msg("服务器内部错误，请联系管理员~", {icon: 2,position:"topCenter"});
                            }
                        }
                    )
                } else {
                    parentId = 0;
                    filePath = rootPath;
                    searchTable(parentId);
                }
            }
            //刷新
            , refresh: function () {
                parentId = 0;
                filePath = rootPath;
                checkObj = "";
                $('#tv-current-position').text("/");
                searchTable(0);
            }
            //新建文件夹
            , addFolder: function () {
                layer.open({
                    type: 2,
                    id: "addFolder",
                    skin: 'layer-ext-myskin',
                    title: "新建文件夹",
                    shadeClose: false,
                    shade: 0.3,
                    area: ["460px", "245px"],
                    content: basePath + "/view/system/file/file_add_update_folder?type=1&parentId=" + parentId,
                    end: function () {
                        searchTable(parentId);
                    }
                });
            },
            //上传
            upload: function () {
                layer.open({
                    type: 2,
                    id: "uploadFile",
                    skin: 'layer-ext-myskin',
                    title: "文件上传",
                    shadeClose: false,
                    shade: 0.3,
                    area: ["50%", "60%"],
                    content: basePath + "/view/system/file/file_add?parentId=" + parentId,
                    end: function () {
                        searchTable(parentId);
                    }
                });
            },
            //目录
            config: function () {
                layer.open({
                    type: 2,
                    title: "目录管理",
                    id: 'file_config',
                    skin: 'layer-ext-myskin',
                    shadeClose: false,
                    shade: 0.3,
                    area: ["30%", "80%"],
                    content: basePath + '/view/system/file/file_config',
                    end: function () {
                        searchTable(parentId);
                    }
                });
            }
        }
    });
</script>
</body>
</html>
